package com.loveprogrammer.battle;

import com.alibaba.fastjson2.JSON;
import com.loveprogrammer.battle.action.Action;
import com.loveprogrammer.battle.action.Attack;
import com.loveprogrammer.battle.action.GroupAttack;

import java.util.*;

/**
 * @ClassName Battle
 * @Description 多对多还是一样的效果，就是两个角色集合进行打拼，如果某个角色集合的角色都阵亡了，就结束
 * @Author admin
 * @Date 2024/2/20 17:03
 * @Version 1.0
 */
public class BattleManyToManyTwo {

    // 队列不变
    private final LinkedList<Attack> actions = new LinkedList<>();

    private int addAction(Attack action){
        actions.offer(action);
        return actions.size();
    }

    public void fight(List<Hero> listA, List<Hero> listB) throws InterruptedException {
        // 先初始化
        listA.sort(Comparator.comparing(Hero::getSpeed).reversed());
        for (int i = 0; i < listA.size(); i++) {
            addAction(new GroupAttack(listA.get(i),listB));
        }

        // 再放入listB
        listB.sort(Comparator.comparing(Hero::getSpeed).reversed());
        for (int i = 0; i < listB.size(); i++) {
            GroupAttack attack = new GroupAttack(listB.get(i), listA);
            insertAction(attack);
        }

        // 如果A集合和B集合的生命值都还大于0

        while(listA.stream().anyMatch(e -> e.getHp() > 0) && listB.stream().anyMatch(e -> e.getHp() > 0)) {
            Attack pop = actions.pop();
            boolean run = pop.run();
            if(run) {
                // 再放进去
                if (pop.checkContinue()) {
                    // 要重新计算interval的时间
                    pop.setIntervalTime(pop.getIntervalTime() + pop.computeInterval(pop.speed()));
                    insertAction(pop);
                }
                // 打印
                // System.out.println("A集团 :" + JSON.toJSONString(listA));
                // System.out.println("B集团 :" + JSON.toJSONString(listB));
            }

        }

        if(listA.stream().anyMatch(e -> e.getHp() > 0)) {
            System.out.println("A集团 获胜:" + JSON.toJSONString(listA));
        }else{
            System.out.println("B集团 获胜:" + JSON.toJSONString(listB));
        }
    }

    private void insertAction(Attack attack) {
        int intervalTime = attack.getIntervalTime();

        // 如果第一个就大于attack的interval
        if(actions.get(0).getIntervalTime() > attack.intervalTime()){
            // 在头插入一个
            actions.push(attack);
        }
        else {
            ListIterator<Attack> iterator = actions.listIterator();
            while (iterator.hasNext()) {
                Attack next = iterator.next();

                if (next.getIntervalTime() > intervalTime) {
                    break;
                }
            }
            // 在指定位置插入新元素
            iterator.add(attack);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        BattleManyToManyTwo battle = new BattleManyToManyTwo();

        Hero A = new Hero("A");
        Hero B = new Hero("B");
        B.setSpeed(2000);
        B.setAttack(20);

        Hero C = new Hero("C");
        C.setSpeed(500);
        C.setAttack(20);

        Hero D = new Hero("D");
        D.setSpeed(10);
        D.setAttack(15);
        battle.fight(Arrays.asList(A,C),Arrays.asList(B,D));
    }

}
